function substring(num) {
  if (!num) {
    let min = 0;
    return `${min}分`;
  } else {
    let string = (num / 60).toFixed(2).toString();
    let point = string.indexOf('.');
    let startIndex = 0;
    let hours = string.substring(startIndex, point);
    let minute = (parseFloat(string.substring(point, string.length) * 60)).toFixed(0);
    if (hours!='-0') {
      return `${hours}小时${minute}分`;
    } else {
      return `-${minute}分`
    };
  };
}

let formatDate = date => {
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()

  return [year, month, day].map(formatNumber).join('-')
}

let formatTime = date => {
  let hour = date.getHours()
  let minute = date.getMinutes()
  return [hour, minute].map(formatNumber).join(':')
}

//获取几天后的时间
let getnextDate = num => {
  let date = new Date();
  date.setDate(date.getDate() + num); //获取num天后的日期 
  let y = date.getFullYear();
  let m = date.getMonth() + 1; //获取当前月份的日期 
  let d = date.getDate();
  return [y, m, d].map(formatNumber).join('-')
}

//日期转时间戳  format:2018-11-01 字符串格式日期
let strdate2stamp = date => {
  let dateArr, y, m, d, ndate;
  dateArr = date.split('-');
  y = dateArr[0]; //获取当前日期的年份
  m = dateArr[1] - 1; //获取当前日期的月份
  d = dateArr[2]; //获取当前日期的日
  ndate = new Date(y, m, d);
  return ndate.getTime() / 1000;
}


const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

export {
  substring,
  formatDate,
  formatTime,
  getnextDate,
  strdate2stamp
}